package sw6.digipecs.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteQuery;

public class SentenceBoardStateCursor extends SQLiteCursor {
	
	// TODO: Update all documentation
	
	private static final String[] mColumns = { DBAdapter.DB_SentenceBoard_Index, DBAdapter.DB_SentenceBoard_Picture_ID, DBAdapter.DB_SentenceBoard_Lock};
	
	private static class Factory implements CursorFactory {
		@Override
		public Cursor newCursor(SQLiteDatabase db,
				SQLiteCursorDriver masterQuery, String editTable,
				SQLiteQuery query) {
			return new SentenceBoardStateCursor(db, masterQuery, editTable, query);
		}
	}
	
	public static SentenceBoardStateCursor queryAll(SQLiteDatabase mDb) {
		return (SentenceBoardStateCursor) mDb.queryWithFactory(new Factory(), true, DBAdapter.DB_SentenceBoard, mColumns, null, null, null, null, null, null);
	}
	
	public static SentenceBoardStateCursor queryIndex(SQLiteDatabase mDb, int index) {
		final String select = DBAdapter.DB_SentenceBoard_Index + " = " + index;
		return (SentenceBoardStateCursor) mDb.queryWithFactory(new Factory(), true, DBAdapter.DB_SentenceBoard, mColumns, select, null, null, null, null, null);
	}
	
	private int mIndexIndex;
	private int mPictureIDIndex;
	private int mLock;

	public SentenceBoardStateCursor(SQLiteDatabase db,
			SQLiteCursorDriver driver, String editTable, SQLiteQuery query) {
		super(db, driver, editTable, query);
		
		mIndexIndex = getColumnIndexOrThrow(DBAdapter.DB_SentenceBoard_Index);
		mPictureIDIndex = getColumnIndexOrThrow(DBAdapter.DB_SentenceBoard_Picture_ID);
		mLock = getColumnIndexOrThrow(DBAdapter.DB_SentenceBoard_Lock);
	}
	
	
	/**
	 * Get the index from a cursor.
	 * 
	 * This is used in combination with fetchSentenceBoardState(). This extracts the index from the cursor, that is the position within the sentence board the picture should be positioned.
	 * 
	 * @return An integer representing the index, within the sentence board.
	 */
	public int getSentenceBoardStateIndex() {
		return getInt(mIndexIndex);
	}
	
	/**
	 * Get the picture id  from a cursor.
	 * 
	 * This is used in combination with fetchSentenceBoardState(). This extracts the picture_id from the cursor, that is the unique id of a picture.
	 * 
	 * @return An integer representing the index, within the sentence board.
	 */
	public int getSentenceBoardStatePictureID() {
		return getInt(mPictureIDIndex);
	}
	
	/**
	 * Get the locked state from a cursor.
	 * 
	 * This is used in combination with fetchSentenceBoardState(). This extracts the truth value from the cursor, that is the locked state of the picture.
	 * 
	 * @return An boolean containing true or false. true if the picture has been locked in sentence board and false if the picture is not locked.
	 */
	public boolean getSentenceBoardStateLock(){
		final int lock = getInt(mLock);
		return (lock == 1 ? true : false);
	}
}
